﻿Public Class QuanLyBanvb
    Dim c As CBan = Nothing
    Private Sub QuanLyBanvb_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Load_KhuVuc()

    End Sub
    Sub Load_KhuVuc()
        Dim list As List(Of CKhuVuc) = KhuVucDAO.LayDanhSachKhuVuc()
        cboKVBan.DataSource = list
        cboKVBan.DisplayMember = "MaKV"
        cboKVBan.DisplayMember = "TenKV"



    End Sub

    Private Sub btnThemBan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThemBan.Click
        Dim cb As New CBan
        cb.MaKV = CType(cboKVBan.SelectedItem, CKhuVuc).MaKV
        cb.TenBan = txtTenBan.Text
        Dim kq As Integer = BanDAO.Insert(cb)
        If kq > 0 Then
            MessageBox.Show("Them Thanh Coong!")
            cboKVBan_SelectedIndexChanged(sender, e)
        Else
            MessageBox.Show("Them That Bai")
        End If



    End Sub

    Private Sub cboKVBan_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboKVBan.SelectedIndexChanged

        Dim ma As Integer = CType(cboKVBan.SelectedItem, CKhuVuc).MaKV
        load_banMaKV(ma)

    End Sub
    Sub load_banMaKV(ByVal ma As Integer)
        lvBan.Items.Clear()
        Dim list As List(Of CBan) = BanDAO.LayDanhSachByMaKV(ma)

        For Each i As CBan In list
            Dim l As New ListViewItem(i.MaBan)
            l.SubItems.Add(i.TenBan)
            l.SubItems.Add(i.MaKV)
            l.Tag = i
            lvBan.Items.Add(l)
        Next

    End Sub

    Private Sub btnXoaBan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoaBan.Click
        Try
            If Not c Is Nothing Then
                Dim i As Integer = BanDAO.Delete(c.MaBan)
                If i > 0 Then
                    MessageBox.Show("Xoa Thanh Cong!")
                    cboKVBan_SelectedIndexChanged(sender, e)
                Else
                    MessageBox.Show("Xoa That bai")
                End If
            End If

        Catch ex As Exception

        End Try
    End Sub


    Private Sub DataGridView1_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs)

    End Sub

    Private Sub lvBan_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lvBan.SelectedIndexChanged
        Try
            c = CType(lvBan.SelectedItems(0).Tag, CBan)
            If Not c Is Nothing Then
                txtTenBan.Text = c.TenBan

            End If
        Catch ex As Exception
            ' MessageBox.Show(ex.Message)
        End Try


    End Sub

    Private Sub btnSuaBan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSuaBan.Click

        If txtTenBan.Text <> "" Then
            If Not c Is Nothing Then
                c.TenBan = txtTenBan.Text
                c.MaKV = CType(cboKVBan.SelectedItem, CKhuVuc).MaKV
                Dim kq As Integer = BanDAO.Update(c)
                If kq > 0 Then
                    MessageBox.Show("Cap nhat thanh cong")
                    cboKVBan_SelectedIndexChanged(sender, e)
                Else
                    MessageBox.Show("Cap nhat that bai")
                End If
            End If
           
        Else
            MessageBox.Show("Ten ban ko duoc rong")
        End If



    End Sub
End Class